/**
    ******************************************************************************
    * @file    threadPool.c
    * @author  debian
    * @version V1.0.0
    * @date    2020-07-29
    * @brief   线程池
    ******************************************************************************
    * @attention
    *
    *
    ******************************************************************************
    */ 

/* Includes ------------------------------------------------------------------*/
#include "threadPool.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>

/** @addtogroup DataStruct_Driver
    * @{
    */

/** @addtogroup threadPool
    * @{
    */

/* Private typedef -----------------------------------------------------------*/

/* Private define ------------------------------------------------------------*/

/* Private macro -------------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/

/* Private function prototypes -----------------------------------------------*/

/* Private functions ---------------------------------------------------------*/


/** @defgroup threadPool_Exported_Functions threadPool Exported Functions
    * @{
    */

/** @defgroup threadPool_Exported_Functions_Group1 Initialization and deinitialization functions
    *  @brief    Initialization and Configuration functions
    *
@verbatim    
    ===============================================================================
                ##### Initialization and deinitialization functions #####
    ===============================================================================
    [..]
        This section provides functions allowing to initialize and de-initialize the threadPool
        to be ready for use.
 
@endverbatim
    * @{
    */ 

/**
    * @brief  创建threadPool对象
    * @param  
    * @retval 
    */ 
   void *threadPool_thread(void *arg) 
   {
        threadPool_t *pool = (threadPool_t *)arg;

        async_queue_t *queue = pool->queue;

        while(1) {
            task_t *t = pool->op->pop(queue, 50);
        
            if(t) {
                if(t->run) {
                    t->run(t->arg);
                    printf("threadPool_thread %d\n", *((int *)t->arg));
                }
                pool->op->task_free(t);
            }
        }

       return NULL;
   }


/**
    * @brief  创建threadPool对象
    * @param  
    * @retval 
    */ 
    threadPool_t* threadPool_creat(int thread_count, int queue_size, threadpool_type_t type)
    {
        threadPool_t * pool = malloc(sizeof(threadPool_t));
        if(pool == NULL)  return NULL;

        pool->thread_count = thread_count;
        pool->thread_id = calloc(thread_count, sizeof(pthread_t));
        if(pool->thread_id == NULL)  return NULL;
   
        // 创建队列
        if(type == threadpool_cond_type) {
            pool->op = &async_cond_op;
        }
        
        pool->queue = pool->op->create(queue_size);
        if(pool->queue == NULL)  {
            free(pool->thread_id);
            free(pool);
            return NULL;
        }
        printf("threadPool_creat 1\n");
        int i = 0;
        for(i=0; i<thread_count; i++)
        {
            printf("pthread_create\n");
            if(pthread_create(&pool->thread_id[i], NULL, threadPool_thread, pool) < 0)
            {
                printf("pthread_creat fail %d\n", i);
                threadPool_destroy(pool);
                return NULL;
            }
        }

        return pool;
    }


    int threadPool_add(threadPool_t* pool, void *(*run)(void *), void *arg)
    {
        task_t t = {0};
        t.run = run;
        t.arg = arg;
       
        pool->op->push(pool->queue, &t);
        return 0;
    }



/**
    * @brief  销毁threadPool对象
    * @param  
    * @retval 
    */ 
    int threadPool_destroy(threadPool_t* pool)
    {
        return 0;
    }
    
/**
    * @}
    */

/** @defgroup threadPool_Exported_Functions_Group2 operation functions 
    *  @brief   operation functions
    *
@verbatim   
    ===============================================================================
                        ##### operation functions #####
    ===============================================================================
    [..]
        This subsection provides a set of functions allowing to manage the threadPool.

@endverbatim
    * @{
    */

        /* 操作函数写在这里 */

    /**
    * @}
    */


/**
    * @}
    */

/**
    * @}
    */

/**
    * @}
    */

/************************ (C) 2020 HOME 深圳龙华 *****END OF FILE****/

